package com.hongyuan.venue.ui.person.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.hongyuan.venue.R;
import com.hongyuan.venue.base.AppViewModelFactory;
import com.hongyuan.venue.base.CustomActivity;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.data.userbean.FileBean;
import com.hongyuan.venue.databinding.ActivityEditInformationBinding;
import com.hongyuan.venue.myview.time_selecter.use_time.GetTimeData;
import com.hongyuan.venue.ui.person.view_mode.EditInforMationViewModel;
import com.hongyuan.venue.utils.CustomDialog;
import com.hongyuan.venue.utils.GlideEngine;
import com.hongyuan.venue.utils.TimeUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class EditInformationActivity extends CustomActivity<ActivityEditInformationBinding, EditInforMationViewModel> {

    //生日选择器
    private GetTimeData birthDay;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_edit_information;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public EditInforMationViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(EditInforMationViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();

        setOnRefresh(binding.refresh,REFRESH_0X4);
        binding.comBack.setOnClickListener(view -> finish());

        //初始化生日选择器
        birthDay = new GetTimeData(this, (date, v) -> {
            String birth_date = TimeUtil.getStringByFormat(date,TimeUtil.dateFormatYMD);
            binding.birthday.setText(TimeUtil.getStringByFormat(date,TimeUtil.dateFormatYMD));

            viewModel.setParams("birth_date",birth_date);
            viewModel.requestData(Constants.UPDATE_MEMBER_BIRTH);
            viewModel.entity.get().setBirth(birth_date);
            viewModel.entity.notifyChange();
        }, TimeUtil.getCurrentDateFormat(),TimeUtil.getCurrentDateFormat(),GetTimeData.YMD);
    }

    @Override
    public void initData() {
        super.initData();

        //请求自己的信息
        viewModel.requestData(Constants.GET_MEMBER_INDEX_INFO);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.openPhoto.observe(this, aVoid -> {
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                    .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                    .enableCrop(true)// 是否裁剪
                    .compress(true)// 是否压缩
                    .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                    .isZoomAnim(true)//图片列表点击 缩放效果 默认true
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        });

        viewModel.uc.changeNike.observe(this, aVoid -> {
            CustomDialog.updateName(this, (v1, message) -> {
                viewModel.setParams("m_name",message);
                viewModel.requestData(Constants.UPDATE_MEMBER_NAME);
                viewModel.entity.get().setM_name(message);
                viewModel.entity.notifyChange();
            });
        });

        viewModel.uc.changeSex.observe(this, aVoid -> {
            CustomDialog.updateSex(this,viewModel.entity.get().getMi_sex(), (v12, message) -> {
                viewModel.setParams("sex",message);
                viewModel.requestData(Constants.UPDATE_MEMBER_SEX);
                viewModel.entity.get().setMi_sex(Integer.parseInt(message));
                viewModel.entity.notifyChange();
            });
        });

        viewModel.uc.changeBirth.observe(this, aVoid -> {
            birthDay.showTime();
        });

        viewModel.uc.changeReName.observe(this, aVoid -> {
            CustomDialog.updateName(this, (v1, message) -> {
                viewModel.setParams("mi_realname",message)
                        .setParams("mi_sex",String.valueOf(viewModel.entity.get().getMi_sex()))
                        .setParams("mi_birth",viewModel.entity.get().getBirth());

                viewModel.requestData(Constants.UPDATE_M_INFO);
                viewModel.entity.get().setMi_realname(message);
                viewModel.entity.notifyChange();
            });
        });

        viewModel.uc.changeSign.observe(this, aVoid -> {
            CustomDialog.updateSign(this, (v1, message) -> {
                viewModel.setParams("sign",message);
                viewModel.requestData(Constants.UPDATE_MEMBER_SIGN);
                viewModel.entity.get().setMi_sign(message);
                viewModel.entity.notifyChange();
            });
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    Glide.with(this).load(selectList.get(0).getCompressPath()).into(binding.headImg);

                    //上传单个文件
                    if(selectList.get(0).getCompressPath() != null){
                        FileBean fileBean = new FileBean();
                        fileBean.setFileKey("oss_file");
                        fileBean.setFilePath(selectList.get(0).getCompressPath());
                        fileBean.setFileType(selectList.get(0).getMimeType());
                        fileBean.setFileUri(Uri.parse(selectList.get(0).getCompressPath()));
                        fileBean.setmFile(new File(selectList.get(0).getCompressPath()));

                        viewModel.updateFile(fileBean);
                    }
                    break;
            }
        }
    }
}
